package com.qdlc.p2p.manage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserCacheService;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.UserModel;

/**
 * 用户管理
 * 
 * @author sj
 * @version 2.0
 * @since 2014年4月17日
 */
public class ManageUserAction extends BaseAction<UserModel> implements ModelDriven<UserModel> {

	@Resource
	private UserService userService;
	@Resource
	private UserIdentifyService userIdentifyService;
	@Resource
	private UserCacheService userCacheService;

	private Map<String, Object> data;

	/**
	 * 获得用户清单页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/user/userManager")
	public String userManager() throws Exception {
		return "userManager";
	}

	/**
	 * 获得用户清单列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/user/userList")
	public void userList() throws Exception {
		data = new HashMap<String, Object>();
		int pageNumber = paramInt("page");// 当前页数
		int pageSize = paramInt("rows");// 每页总数
		String status = request.getParameter("status");
        if (status == null) {
        	model.setStatus(99);
        } 
		PageDataList<UserModel> pagaDataList = userService.userList(pageNumber, pageSize, model);
		int totalPage = pagaDataList.getPage().getTotal();// 总页数
		data.put("total", totalPage);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 锁定用户页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/user/userLockEditPage")
	public String userLockEditPage() throws Exception {
		long userId = paramLong("id");
		UserCache userCache = userCacheService.findByUserId(userId);
		request.setAttribute("userCache", userCache);
		return "userLockEditPage";
	}

	/**
	 * 锁定用户 1锁定 0未锁定
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/user/userLockEdit")
	public void userLockEdit() throws Exception {
		data = new HashMap<String, Object>();
		UserCache userCache =  userCacheService.findById(model.getId());
        userCache.setStatus(model.getStatus());
        userCache.setRemark(paramString("remark"));
        userCache.setLoginFailTimes(0);
        userCache.setLockTime(DateUtil.getNow());
        userCacheService.update(userCache);
		data.put("result", true);
		data.put("msg", "修改成功！");
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 修改用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/user/userEditPage")
	public String userEditPage() throws Exception {
		long userId = paramLong("id");
		User user = userService.find(userId);
		UserIdentify userIdentify = userIdentifyService.findByUserId(userId);
		UserCache userCache = userCacheService.findByUserId(userId);
		request.setAttribute("user", user);
		request.setAttribute("userAttestation", userIdentify);
		request.setAttribute("userCache", userCache);
		return "userEditPage";
	}
	
	/**
	 * 获得用户列表
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/user/user/user/userListPage")
	public String getUserListPage() throws Exception {
		return "userListPage";
	}

	/**
	 * 导出EXCEL列表
	 */
	@Action("/modules/user/user/user/doExportExcel")
	public void doExportExcel() throws Exception{
		int count = userService.getCountByUserCondition(model);
		String title = "user_info";
		List<TableData> tableDatas = new ArrayList<TableData>();
		int itemCount = 5000; //每个文件用户数
		int page = new Double(count / itemCount).intValue() + 1; //总文件数
		for(int i = 0; i < page; i++) {
			List<UserModel> userList = userService.userListForExcel(i * itemCount, itemCount, model);
			String[] hearders = new String[] { "编号", "用户名", "真实姓名", "邮箱", "手机号码", "身份证号码","实名状态(1为通过)","锁定状态(true为锁定)","注册时间" };// 表头数组
			String[] fields = new String[] { "userId", "userName", "realName","email", "mobilePhone", "cardId","realNameStatus","status","addTime"};// 对象属性数组
			TableData td = ExcelUtils.createTableData(userList, ExcelUtils.createTableHeader(hearders), fields);
			tableDatas.add(td);
		}
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToZipExcel(title, getOperator().getUserName(), tableDatas);
	}
}
